package com.foreknow.dao.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Guestbook;
import com.foreknow.util.MysqlPagination;
import com.foreknow.util.Pagination;
import com.sun.corba.se.spi.orbutil.fsm.GuardBase;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDAO implements GuestbookDao {

    @Override
    public boolean save(Guestbook gb) throws SQLException {
        String sql = "insert into guestbook(name,phone,email,title,content,createdtime) values(?,?,?,?,?,?)";
        int i = jdbcTemplate.update(sql,gb.getName(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent(),gb.getCreatedtime());
        return i==1;
    }

    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql = "select * from guestbook";
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping);
        return list;
    }



    @Override
    public Guestbook getQueryById(int id) throws SQLException {
        String sql = "select * from guestbook where id=?";
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,id);
        if(list.size()>0) {
            Guestbook gb = (Guestbook) list.get(0);
            return gb;
        }
        return null;
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        String sql = "delete  from guestbook where id=?";
        int isRigth = jdbcTemplate.update(sql,id);
        return isRigth==1;
    }

    @Override
    public boolean update(Guestbook gb) throws SQLException {
        String sql="update guestbook set name=?,phone=?,email=?,title=?,content=?,createdtime=? where id=? ";
        int update = jdbcTemplate.update(sql, gb.getName(), gb.getPhone(), gb.getEmail(), gb.getTitle(), gb.getContent(), gb.getCreatedtime(), gb.getId());
        return update==1;

    }

    @Override
    public Pagination getGuestbookByPage(int pageSize, int pageNumber) throws SQLException {
        String sql="select * from guestbook";
        Pagination pagination=new MysqlPagination(sql,pageSize,pageNumber);

        return pagination;
    }

//    public static void main(String[] args) {
//        GuestbookDao dao = new GuestbookDaoImpl();
//        try {
//            Guestbook gb =  dao.getQueryById(6);
//            System.out.println(gb.getName());
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
////        try {
////            List<Object> list = dao.getQueryAll();
////            for(int i = 0;i<list.size();i++){
////                Guestbook gb =  (Guestbook) list.get(i);
////                System.out.println(gb.getName());
////            }
////        } catch (SQLException e) {
////            e.printStackTrace();
////        }
////        Guestbook gb = new Guestbook();
////        gb.setName("tom");
////        gb.setPhone("12121212");
////        gb.setEmail("tom@163.com");
////        gb.setTitle("tom test");
////        gb.setContent("content......");
////        try {
////            boolean isRight = dao.save(gb);
////            if(isRight){
////                System.out.println("添加成功......");
////            }else {
////                System.out.println("添加失败......");
////            }
////        } catch (SQLException e) {
////            e.printStackTrace();
////        }
//    }



}
